package com.pic.mgt.haishin.action.shell;

import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.Options;

import com.pic.mgt.haishin.action.HaishinBusinessResendAction;
import com.pic.mgt.telnetd.AbstractCommandPerformer;

public class HaishinBusinessResendCommandPerformer extends
		AbstractCommandPerformer {
	
	private final String commandName = "rsdbiz";
	private final String commandDescritpion = "resend a business. (NOTICE: the execution status of business must be successful)";
	
	private HaishinBusinessResendAction haishinBusinessResendAction;

	@Override
	protected void addOption(Options options) {
		options.addOption("tn", true, "assign target file name. (required)");
		options.addOption("t", true, "assign terminal id. (required)");
	}

	@Override
	protected String doCommandPerform(CommandLine commandLine) {
		if (!commandLine.hasOption("tn") || !commandLine.hasOption("t")) {
			return
			super.getReplyMessageWithReplyCode("did not provide target file name or terminal id.",
											   this.COMMAND_PERFORM_FAILED_CODE);
		}
		
		// -------------------------------------------

		boolean isResendSuccessful = 
			this.haishinBusinessResendAction.resendBusinessToTerminal(
											commandLine.getOptionValue("tn"),
											commandLine.getOptionValue("t"));

		if (isResendSuccessful) {
			return super.getCommandPerformFinishMsg();
		}
		else {
			return super.getCommandPerformFailedMsg();
		}
	}

	@Override
	public String getCommandDescription() {
		return this.commandDescritpion;
	}

	@Override
	public String getCommandName() {
		return this.commandName;
	}

	public void setHaishinBusinessResendAction(
			HaishinBusinessResendAction haishinBusinessResendAction) {
		this.haishinBusinessResendAction = haishinBusinessResendAction;
	}

}
